package com.ke.forum.forumapi.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ke.forum.common.common.Result;
import com.ke.forum.mbg.entity.UserFollow;
import com.ke.forum.mbg.dao.UserFollowMapper;
import com.ke.forum.forumapi.service.UserFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ke.forum.mbg.entity.dto.UserDto;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 珂
 * @since 2021-05-22
 */
@Service
public class UserFollowServiceImpl extends ServiceImpl<UserFollowMapper, UserFollow> implements UserFollowService {

    @Autowired
    UserFollowMapper userFollowMapper;

    @Override
    public Result addStar(String upUuid) {
        UserDto userDto = (UserDto) SecurityUtils.getSubject().getPrincipal();
        if (upUuid.equals(userDto.getUuid())){
            return Result.FAIL("不能关注自己");
        }
        UserFollow uf = userFollowMapper.selectOne(Wrappers.<UserFollow>lambdaQuery().eq(UserFollow::getUpUuid, upUuid).eq(UserFollow::getUserUuid, userDto.getUuid()));
        if (uf!=null){
            return Result.FAIL("已关注该用户");
        }
        UserFollow userFollow = new UserFollow();
        userFollow.setChecked(0);
        userFollow.setUpUuid(upUuid);
        userFollow.setUserUuid(userDto.getUuid());
        userFollowMapper.insert(userFollow);
        return Result.SUCCESS(true);
    }

    @Override
    public Result deleteStar(String upUuid) {
        UserDto userDto = (UserDto) SecurityUtils.getSubject().getPrincipal();
        userFollowMapper.delete(Wrappers.<UserFollow>lambdaQuery().eq(UserFollow::getUpUuid, upUuid).eq(UserFollow::getUserUuid, userDto.getUuid()));
        return Result.SUCCESS(true);
    }
}
